// Copyright (c) Huawei Technologies Co., Ltd. 2023-2024. All rights reserved.
// licensed under the Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//      http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
// PURPOSE.
// See the Mulan PSL v2 for more details.
import type { DistributionParams, PageNation, Sort } from '../types'

export type CveLevel = 'Critical' | 'High' | 'Medium' | 'Low' | 'Unknown'
export type CveCount = Record<CveLevel, number>
export interface CveFilter {
  affected: boolean
  fixed: boolean
  search_key?: string
  severity: string[]
}
export interface Cve {
  cve_id: string
  cvss_score: string
  description: string
  host_num: number
  package: string
  publish_time: string
  severity: string
  rpms?: Rpm[]
}
export interface CveTableItem {
  cve_id: string
  cvss_score: string
  description: string
  host_num?: number
  package: string
  publish_time: string
  severity: string
  rpms?: Rpm[]
}
export interface UnfixedRpm {
  cve_id?: string
  available_rpm: string
  installed_rpm: string
  host_num: number
  support_way: string
  status?: string
}
export interface FixedRpm {
  cve_id?: string
  host_num: number
  installed_rpm: string
  hp_status: string
  fixed_way: string
}
export type Rpm = UnfixedRpm & FixedRpm
export interface Package {
  os_version: string
  package: string
}
export interface CveDetail {
  cve_id: string
  cvss_score: string
  description: string
  package: Package[]
  publish_time: string
  related_cve: string[]
  severity: string
}
// #region ------------------------------------< request params >------------------------------------
export type generateCveFixTaskParams = DistributionParams<{
  task_name: string
  description: string
  accepted: boolean
  takeover: boolean
  check_items: string[]
  info: {
    cve_id: string
    host_info: {
      host_id: string
    }[]
    rpms: {
      available_rpm: string
      installed_rpm: string
      fix_way: string
    }[]
  }[]
}>
export type generateCveHotPatchTaskParams = DistributionParams<{
  task_name: string
  description: string
  info: {
    host_id: string
    cves: { cve_id: string }[]
  }[]
}>
export interface generateRepoSetTaskParams {
  task_name: string
  description: string
  repo_id: string
  host_list: string[]
}
export type QueryCvesParams = {
  filter: Partial<CveFilter>
} & Partial<PageNation> &
Partial<Sort>
export type QueryCvesWithHostParams = {
  host_id: string
  filter: Partial<CveFilter>
} & Partial<PageNation> &
Partial<Sort>
interface CveHostFilter {
  fixed: boolean
  host_group_ids?: string[]
  host_name?: string
  repo?: string[]
  cluster_list?: string[]
}
export type QueryHostWithIdParams = {
  cve_id: string
  filter: CveHostFilter
} & Partial<PageNation> &
Partial<Sort>

interface QueryTasksFilter {
  task_name: string
  task_type: string[]
}
export type QueryTasksParams = { filter?: Partial<QueryTasksFilter> } & Partial<Sort> &
  Partial<PageNation>
export type QueryTasksHostsParams = Partial<PageNation> & {
  task_id: string
  filter: { search_key?: string, status?: string[] }
}
// #endregion

// #region ------------------------------------< response data >------------------------------------
interface HostFixStatus {
  fixed_cve_num: number
  unfixed_cve_num: number
}
interface HostAffectedStatus {
  affected_cve_num: number
  unaffected_cve_num: number
}
export interface Host {
  host_id: string
  host_name: string
  host_ip: string
  host_group: string
  repo: string | null
  last_scan: string
  fixed_cve_num: number
  unfixed_cve_num: number
  cluster_id: string
  cluster_name: string
}
export type AffectedHost = Omit<Host, keyof HostFixStatus> & {
  rpms?: Rpm[]
}
export interface Repo {
  repo_id: number
  repo_attr: string
  repo_data: string
  repo_name: string
  cluster_id: string
  cluster_name: string
}
export interface HostDetail extends HostFixStatus, HostAffectedStatus {
  host_ip: string
  host_name: string
  host_group: string
  last_scan: number
  repo_id: string | null
  repo_name: string | null
  reboot?: boolean
  cluster_id: string
  cluster_name: string
}
export interface TaskStatus {
  succeed: number
  fail: number
  running: number
  unknown: number
}
export interface Task {
  task_id: string
  task_name: string
  task_type: string
  host_num: number
  description: string
  create_time: number
  status?: TaskStatus
  cluster_id: string
  cluster_name: string
}
export interface TaskDetail {
  task_name: string
  description: string
  cluster_id: string
  cluster_name: string
  accept: boolean
  takeover: boolean
  host_num: number
  latest_execute_time: number | null
  status?: TaskStatus
}
export interface CveFixInfo {
  host_id: string
  host_ip: string
  host_name: string
  status: string
  cve_num: number
  rpms?: Rpm[]
}
export interface RepoSetInfo {
  host_id: string
  host_ip: string
  host_name: string
  repo_name: string
  status: string
}
export type CveRollbackInfo = CveFixInfo
export interface HotPatchInfo {
  cve_id: string
  host_num: number
  package: string
  status: string
}
export interface TaskResultRpm {
  available_rpm: string
  log: string
  result: string
}
export interface TaskResult {
  check_items: { item: string, result: boolean }[]
  dnf_event_start: number
  dnf_event_end: number
  timed: boolean
  rpms?: TaskResultRpm[]
}
// export interface TaskResultReport {
//   task_id: string;
//   task_type: string;
//   host_id: number;
//   host_ip: string;
//   host_name: string;
//   latest_execute_time: number;
//   status: string;
//   task_result: TaskResult;
// }
export interface TaskReport {
  task_id: string
  task_type: string
  host_id: string
  latest_execute_time: number
}
export interface RepoTaskResult {
  check_items: { item: string, result: boolean }[]
  host_ip: string
  host_name: string
  log: string
  repo: string
  status: string
}
export interface CveTaskResult {
  check_items: { item: string, result: boolean }[]
  rpms: { available_rpm: string, result: string, log: string }[]
}
export interface CveRollbackTaskResult {
  result: string
  log: string
  check_items: { item: string, result: boolean }[]
  rpms: { cves: string, installed_rpm: string, target_rpm: string }[]
}
export interface HotPatchRemoveTaskResult {
  timed: boolean
  check_items?: { item: string, result: boolean }[]
  cves: {
    cve_id: string
    result: string
    log: string
  }[]
}
export interface RepoTaskReport extends TaskReport {
  task_result: RepoTaskResult
}

export interface CveTaskReport extends TaskReport {
  host_ip: string
  host_name: string
  status: string
  task_result: CveTaskResult
}
export interface CveRollbackTaskReport extends TaskReport {
  host_ip: string
  host_name: string
  status: string
  task_result: CveRollbackTaskResult
}
export interface HotPatchRemoveTaskReport extends TaskReport {
  host_ip: string
  host_name: string
  status: string
  task_result: HotPatchRemoveTaskResult
}

// #endregion
